CygNet Studio > Scripting in CygNet Studio > Scripting Examples > Adding Automatic Polling to a Screen

Adding Automatic Polling to a Screen

A simple polling interval section can be added to any screen to which polling applies.

Polling interval

An invisible timer issues a poll command on the specified polling interval for a specified duration, then stops. The Polling Duration drop-down menu also has an option to poll indefinitely on the given interval.

The following example shows how an auto polling control panel can be set up.

To Add Automatic Polling to a Screen

  1. Create a new Studio screen.
  2. Add the following controls:
  1. A second Combo Box, with the (ObjectCode) set to cboDuration.
  2. An ActiveX control (choose iTimersX), with the (ObjectCode) set to timer.
  1. Add elements to the Combo Boxes.
  1. In the properties for cboInterval, click the ListItems field. Enter the following items: “5 seconds,” “10 seconds,” and “30 seconds.”  These intervals can be modified to suit your application. Set NumDropped to 3.
  2. In the properties for cboInterval, click the ListItems field. Enter the following items: “1 minute,” “2 minutes,” “5 minutes,” and “Forever.” These durations can be modified to suit your application. Set NumDropped to 4.
  1. In the properties for the Timer, set Visible to No.
  2. Add initialization script.
  1. Open the Script Editor and navigate to the chkEnable EventInitialize event.
  2. Enter the following script to set the Check * to unchecked.

Sub chkEnable_EventInitialize()

Dim This : Set This = chkEnable

this.Check = 0

End Sub

  1. Navigate to the cboInterval EventInitialize event.
  2. Enter the following script to ensure that the first item is selected.

Sub cboInterval_EventInitialize()

Dim This : Set This = cboInterval

This.Selection = 0

End Sub

  1. Navigate to the cboDuration EventInitialize event.
  2. Enter the following script to ensure that the first item is selected.

Sub cboDuration_EventInitialize()

Dim This : Set This = cboDuration

This.Selection = 0

End Sub

  1. Add script to the Check Box.
  1. Navigate to the chkEnable EventChange event.
  2. Enter the following script. This script runs when a user checks or unchecks the Check Box. The cboDuration control is analyzed and the timer’s interval (number 2) is set accordingly. Timer intervals are measured in milliseconds.

Sub chkEnable_EventChange()

Dim This : Set This = chkEnable

If This.Check = True Then

timer.Enabled1 = True

timer.Enabled2 = True

strTime = FormatDateTime(Now(),0)

If cboDuration.Selection = 0 Then '1 minute

timer.Interval2 = (60 * 1000)

End If

If cboDuration.Selection = 1 Then  '2 minutes

timer.Interval2 = (120 * 1000)

End If

If cboDuration.Selection = 2 Then  '5 minutes

timer.Interval2 = (300 * 1000)

End If

If cboDuration.Selection = 3 Then  'Indefinitely

timer.Interval2 = (500 * 1000)

End if

Else

timer.Enabled1 = False

timer.Enabled2 = False

End If

End Sub

  1. Add script to the Combo Boxes.
  1. Navigate to the cboDuration EventChange event.
  2. Enter the following script. This script runs when a user changes the selection in the duration Combo Box. The timer control’s interval (number 2) is updated accordingly.

Sub cboDuration_EventChange()

Dim This : Set This = cboDuration

If This.Selection = 0 Then  '1 minute

timer.Interval2 = (60 * 1000)

End If

If This.Selection = 1 Then  '2 minutes

timer.Interval2 = (120 * 1000)

End If

If This.Selection = 2 Then  '5 minutes

timer.Interval2 = (300 * 1000)

End If

If This.Selection = 3 Then  'Indefinitely

timer.Interval2 = (500 * 1000)

End if

End Sub

  1. Navigate to the cboInterval EventChange event.
  2. Enter the following script. This script runs when a user changes the selection in the interval Combo Box. The timer control’s interval (number 1) is updated accordingly.

Sub cboInterval_EventChange()

Dim This : Set This = cboInterval

If This.Selection = 0 Then  '5 seconds

timer.Interval1 = (5 * 1000)

End If

If This.Selection = 1 Then  '10 seconds

timer.Interval1 = (10 * 1000)

End If

If This.Selection = 2 Then  '30 seconds

timer.Interval1 = (30 * 1000)

End If

End Sub

  1. Add script to the Timer events.
  1. Navigate to the timer EventOnTimer1 event.
  2. Enter the following script. The event uses the global inTimer Boolean variable to determine whether or not the process within the timer is being executed or not, a good practice to use in cases where the events in the timer take longer than the timer interval.

Sub timer_EventOnTimer1()

Dim This : Set This = timer

On Error Resume Next

If inTimer = False Then

inTimer = True

btnPoll.RunClick

inTimer = False

End If

End Sub

  1. Navigate to the timer EventOnTimer2 event.
  2. Enter the following script. This script executes when the timer’s second interval reaches the end of its duration, and turns off the first interval unless the user has it running "forever."

Sub timer_EventOnTimer2()

Dim This : Set This = timer

If cboDuration.Selection <> 3 Then

Timer.Enabled1 = False

End If

End Sub

  1. Add general declarations.
  1. Navigate to the General, Declarations section.
  2. Enter the following script to declare the inTimer variable and provide a method for polling. Insert the poll logic for your facility.

'(Declarations)

 

Dim inTimer

 

Sub DoPoll()

'Custom poll logic

End Sub

 

'End of (Declarations)

  1. Save the screen and switch to Run mode. To test whether or not the timer is firing at the determined rate, a MsgBox command can be added to the DoPoll method (for example, MsgBox("Polling")).

This example can be extended in a number of ways. A Button can be added to poll immediately, or a Label can be added to show a countdown until the current poll duration will stop.

Back to top

Let us know how we can improve this topic.

CygNet at weatherford.com

© 2020 Weatherford. All rights reserved.